package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.PracticeCarSetActivity;
import com.yiren.dbaa.module.practice_set.site.contract.SiteRuleManageContract;
import com.yiren.dbaa.module.practice_set.site.presenter.SiteRuleManagePresenter;
import com.yiren.dbaa.module.practice_set.site.view.dialog.SelectUserDialog;
import com.yiren.dbaa.module.project_edit.view.activity.ProjectManageActivity;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/28 9:34
 * @description: 考场规则管理
 */
public class SiteManageActivity extends BaseActivity implements SiteRuleManageContract.ManageView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_site_name)
    TextView tvSiteName;
    @BindView(R.id.tv_site_rule_v)
    TextView tvSiteRuleV;
    @BindView(R.id.tv_version_bt)
    TextView tvVersionBt;
    @BindView(R.id.tv_official_v)
    TextView tvOfficialV;
    @BindView(R.id.tv_official_version_bt)
    TextView tvOfficialVersionBt;
    @BindView(R.id.tv_using_range)
    TextView tvUsingRange;
    @BindView(R.id.tv_using_range_bt)
    TextView tvUsingRangeBt;
    @BindView(R.id.tv_release)
    TextView tvRelease;
    @BindView(R.id.tv_edit_rule)
    TextView tvEditRule;

    private Intent intent;
    private SiteEntity site;
    private SiteRuleManageContract.Presenter siteRuleManagePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_manage);
        ButterKnife.bind(this);
        tvTitle.setText("考场规则管理");
        intent = getIntent();
        site = (SiteEntity) intent.getSerializableExtra("site");
        init();
    }

    private void init() {
        siteRuleManagePresenter = new SiteRuleManagePresenter(this);
        if (site != null) {
            tvSiteName.setText(site.getSiteName());
            tvSiteRuleV.setText("V" + site.getRuleVer() + "");
            tvOfficialV.setText("V" + site.getBaseVer() + "");
            if (site.getUseRange() == 1) {
                tvUsingRange.setText("个别用户使用");
            } else {
                tvUsingRange.setText("全部用户使用");
            }
            if (site.getRuleVer() != 0) {
                tvVersionBt.setText("回退版本");
                tvVersionBt.setTextColor(getResources().getColor(R.color.white));
                tvVersionBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                if (site.isUpdate()) {
                    tvOfficialVersionBt.setText("    更新    ");
                    tvOfficialVersionBt.setTextColor(getResources().getColor(R.color.white));
                    tvOfficialVersionBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                }
                if (site.getUseRange() == 1) {
                    tvUsingRangeBt.setText("修改范围");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.white));
                    tvUsingRangeBt.setBackgroundResource(R.drawable.btn_blue_radius_selector);
                } else {
                    tvUsingRangeBt.setText("已是全部");
                    tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
                }
            } else {
                tvUsingRange.setText("无用户使用");
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_version_bt, R.id.tv_official_version_bt, R.id.tv_using_range_bt, R.id.tv_release, R.id.tv_edit_rule})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_version_bt:
                if (site.getRuleVer() != 0) {
                    Intent intent = new Intent(this, VersionHistoryActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("site", site);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
                break;
            case R.id.tv_official_version_bt:
                if (site.isUpdate()) {
                    String message;
                    if (site.getUpdateDetails() == null) {
                        message = "";
                    } else {
                        message = site.getUpdateDetails();
                    }
                    CommonDialog.showTwoBtn(this, "更新详情", message, "取消", "立即更新", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (which == -2) {
                                dialog.dismiss();
                            } else {
                                siteRuleManagePresenter.updateSiteRule(site, new CommonCallBack() {
                                    @Override
                                    public void success(Object result) {
                                        site.setBaseVer((Float) result);
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                tvOfficialV.setText("V" + site.getBaseVerLast());
                                                tvOfficialVersionBt.setText("已是最新");
                                                tvOfficialVersionBt.setBackground(null);
                                                tvOfficialVersionBt.setTextColor(getResources().getColor(R.color.gray_text));
                                                CommonDialog.showTwoBtn(SiteManageActivity.this, "操作确认", "更新已完成，请问是否进入编辑规则？", "取消", "确认", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        if (which == -2) {
                                                            finish();
                                                            startActivity(new Intent(SiteManageActivity.this, PracticeCarSetActivity.class));
                                                        } else {
                                                            intent = new Intent(SiteManageActivity.this, ProjectManageActivity.class);
                                                            Bundle bundle = new Bundle();
                                                            bundle.putSerializable("site", site);
                                                            intent.putExtras(bundle);
                                                            startActivity(intent);
                                                        }
                                                    }
                                                });
                                            }
                                        });

                                    }

                                    @Override
                                    public void fail(String error) {
                                        MessageBox.show(SiteManageActivity.this, "更新失败！");
                                    }
                                });
                            }
                            dialog.dismiss();
                        }
                    });
                }
                break;
            case R.id.tv_using_range_bt:  //选择用户
                if (site.getUseRange() == 1) {
                    List<String> user = new ArrayList<>();
                    user.add("全部用户使用");
                    user.add("个别用户使用");
                    SelectUserDialog.show(this, "修改范围", site.getUseRange() == 1 ? 1 : 0, user, new SelectUserDialog.OnSureClickListener() {
                        @Override
                        public void onSureClick(int selectIndex) {
                            siteRuleManagePresenter.changeUserRange(SiteManageActivity.this, site, selectIndex == 0 ? 2 : 1);
                        }
                    });
                }
                break;
            case R.id.tv_release:
                intent = new Intent(this, ReleaseActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("site", site);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_edit_rule:
                intent = new Intent(this, ProjectManageActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("site", site);
                intent.putExtras(bundle1);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void updateUseRange(int range) {
        if (range == 2) {
            tvUsingRange.setText("全部用户使用");
            tvUsingRangeBt.setText("已是全部");
            tvUsingRangeBt.setBackground(null);
            tvUsingRangeBt.setTextColor(getResources().getColor(R.color.gray_text));
        } else {
            tvUsingRange.setText("个别用户使用");
        }
    }
}
